package com.fg.pro.ai.AiController;


import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.fg.pro.model.reponse.ResponseResult;
import com.fg.pro.model.utils.UserIdThread;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SafeGuardAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "aiChat",description = "ai_aiChat")
@RestController
@RequestMapping("aiChat")
public class AiChat {

//    @Autowired
//    ChatClient.Builder chatClientBuilder;
    @Autowired
    DashScopeChatModel dashScopeChatModel;
    @jakarta.annotation.Resource
    DeepSeekChatModel deepSeekChatModel;
//    外部提示词
//    提示词可以去网上找清晰的提示词
    @Value("classpath:promptTemplate.txt")
    Resource resource;

    @Operation(summary = "默认回答",description = "")
    @GetMapping("/chartByBlock")
    public ResponseResult chartByBlock(@RequestParam(required = false)String question){
//        ChatClient.Builder builder = ChatClient.builder(dashScopeChatModel);
//        ChatClient chatClient = builder.build();
        ChatClient chatClient = ChatClient
                .builder(deepSeekChatModel)
                .defaultSystem("系统提示词--{name}{age}")
//                拦截器
//                .defaultAdvisors(new SimpleLoggerAdvisor())
                .defaultAdvisors(new SafeGuardAdvisor(List.of("敏感词1","敏感词2")))
                .build();

        String content = chatClient
                .prompt(question)
                .user(UserIdThread.sysUserId()!=null?UserIdThread.sysUserId().toString():"默认用户")
//                .system("当前对话提示词")
//                提示词模板
                .system(p->p.param("name","jixiang").param("age","18"))
                .call()
                .content();

        return ResponseResult.okResult(content);
    }


    @Autowired
    private ChatMemory chatMemory;

    @Operation(summary = "默认回答（拦截器自动加入记忆体）",description = "")
    @GetMapping("/chartByAdviceAndMemory")
    public ResponseResult chartByAdviceAndMemory(@RequestParam(required = false)String question){
        ChatClient chatClient = ChatClient
                .builder(dashScopeChatModel)
                .defaultAdvisors(
                        PromptChatMemoryAdvisor.builder(chatMemory).build()
                )
                .build();
        String content = chatClient
                .prompt()
                .user(question)
                .call()
                .content();

        return ResponseResult.okResult(content);
    }

    @Operation(summary = "默认回答（拦截器自动加入记忆体-不同用户）",description = "")
    @GetMapping("/chartByAdviceAndMemoryAndUser")
    public ResponseResult chartByAdviceAndMemoryAndUser(@RequestParam(required = false)String question){
        ChatClient chatClient = ChatClient
                .builder(dashScopeChatModel)
                .defaultAdvisors(
                        PromptChatMemoryAdvisor.builder(chatMemory).build()
                )
                .defaultAdvisors(
                        advisorSpec -> advisorSpec.param(ChatMemory.DEFAULT_CONVERSATION_ID,"000001")
                )
                .build();
        String content = chatClient
                .prompt()
                .user(question)
                .call()
                .content();
        return ResponseResult.okResult(content);
    }

//    此配置，当前类生效
//    @Configuration
//    static class Config{
//
////        配置记忆体
//        @Bean
//        ChatMemory chatMemory(ChatMemoryRepository chatMemoryRepository){
//            return MessageWindowChatMemory
//                    .builder()
//                    .maxMessages(10)
//                    .chatMemoryRepository(chatMemoryRepository)
//                    .build();
//        }
//    }



//    @Autowired
//    ChatClient.Builder chartClientBuilder;
//
//    @Autowired
//    DashScopeChatModel dashScopeChatModel;
//
//    @Operation(summary = "测试",description = "")
//    @GetMapping("/test")
//    public ResponseResult test(@RequestParam(required = false)String question){
//        ChatClient chatClient = ChatClient.builder(dashScopeChatModel).build();
//        String str = chatClient.prompt().user(question).call().content();
//        System.out.println(str);
//        return ResponseResult.okResult(str);
//    }
//
//    @Operation(summary = "阻塞模式回答",description = "")
//    @GetMapping("/chartByBlock")
//    public ResponseResult chartByBlock(@RequestParam(required = false)String question){
//        ChatClient chatClient = chartClientBuilder.build();
//        String str = chatClient.prompt().user(question).call().content();
//        System.out.println(str);
//        return ResponseResult.okResult(str);
//    }
//
//    @Operation(summary = "流模式回答",description = "")
//    @GetMapping("/chartByStream")
//    public ResponseResult chartByStream(@RequestParam(required = false)String question){
//        ChatClient chatClient = chartClientBuilder.build();
//        Flux<String> content = chatClient.prompt()
//                .user(question)
//                .stream()
//                .content();
//        String call = new String();
//        for (String s : content.toIterable()) {
//            System.out.println(s);
//            call = call +s;
//        }
//        return ResponseResult.okResult(call);
//    }

}
